package com.atguigu.educms.controller;


import com.atguigu.commonutils.ResultVo;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 后台管理员系统
 * </p>
 *
 * @author atguigu
 * @since 2024-12-13
 */
@RestController
@CrossOrigin
@RequestMapping("/educms/banneradmin")
public class BannerAdminController  {

    @Autowired
    private CrmBannerService bannerService;
    //1.分页查询banner
    @ApiOperation(value = "获取Banner分页列表")
    @GetMapping("pageBanner/{page}/{limit}")
    public ResultVo pageBanner(@PathVariable long page, @PathVariable long limit) {
        Page<CrmBanner> pageBanner = new Page<>(page, limit);
        bannerService.page(pageBanner, null);
        return ResultVo.ok().data("items", pageBanner.getRecords()).data("total", pageBanner.getTotal());
    }
    //2.添加banner
    @ApiOperation(value = "新增Banner")
    @PostMapping("addBanner")
    public ResultVo addBanner(@RequestBody CrmBanner crmBanner) {
        boolean save = bannerService.save(crmBanner);
        if (save) {
            return ResultVo.ok();
        } else {
            return ResultVo.error();
        }
    }
    //通过id获取信息
    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public ResultVo getById(@PathVariable String id) {
        CrmBanner crmBanner = bannerService.getById(id);
        return ResultVo.ok().data("item", crmBanner);
    }

    //修改banner
    @PutMapping("update")
    @ApiOperation(value = "修改Banner")
    public ResultVo updateBanner(@RequestBody CrmBanner crmBanner) {
        boolean b = bannerService.updateById(crmBanner);
        if (b) {
            return ResultVo.ok();
        } else {
            return ResultVo.error();
        }
    }

    @DeleteMapping("remove/{id}")
    @ApiOperation(value = "删除Banner")
    public ResultVo remove(@PathVariable String id) {
        boolean b = bannerService.removeById(id);
        if (b) {
            return ResultVo.ok();
        } else {
            return ResultVo.error();
        }
    }
}

